from flask import Flask,render_template,request
# 导入配置文件
import config
from flask_sqlalchemy import SQLAlchemy

'''
	project_name : 数据库交互系统
	start_time : 2018.5.19
	end_time : 2018.5.20
'''

__author__ = '极简XksA'

app = Flask(__name__)
# 将 app 对象设置 config 里的配置
app.config.from_object(config)

# 初始化一个 SQLAlchemy 对象
db = SQLAlchemy(app)


# 歌手信息对象表
class Singers(db.Model) :
	__tablename = 'singers'
	Sid = db.Column(db.Integer,primary_key = True,autoincrement=True)
	Sname = db.Column(db.String(20),primary_key = True,nullable = False)
	Sage = db.Column(db.Integer,nullable = True)


db.create_all()
@app.route('/')
def hello_world() :
	return render_template('index.html')

@app.route('/db')
def test01() :
	tables = request.args.get('1')
	methons = request.args.get('2')
	if tables == 'singers' and methons == 'add':
		return render_template('add.html')
	elif methons == 'delete' :
		return render_template('delete.html')
	elif methons == 'search' :
		return render_template('search.html')
	elif methons == 'change' :
		return render_template('alter.html')
	else:
		return '正在开发'
	
@app.route('/add')
def singers_add() :
	from search.search_info import add_singers
	sid = int(request.args.get('Sid'))
	sname = request.args.get('Sname')
	sage = int(request.args.get('Sage'))
	add_singers(sid,sname,sage)
	return render_template('index.html')

@app.route('/delete')
def singers_delete() :
	from search.search_info import delete_singers
	sname = request.args.get('Sname')
	delete_singers(sname)
	return render_template('index.html')

@app.route('/search')
def singers_search() :
	from search.search_info import search_singers
	sname = request.args.get('Sname')
	singers = search_singers(sname)
	context = {
		'Sid' : singers.Sid,
		'Sname' : singers.Sname,
		'Sage' : singers.Sage
	}
	return render_template('show.html',**context)

@app.route('/alter')
def singers_alter() :
	from search.search_info import alter_singers
	sname = request.args.get('Sname')
	sage = request.args.get('Sage')
	alter_singers(sname,sage)
	return render_template('index.html')

if __name__ == '__main__':
	app.run()
	
	